package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.query.TravelQuery;
import cn.wolfcode.luowowo.service.ITravelService;
import cn.wolfcode.luowowo.util.JsonResult;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 攻略主题
 */
@Controller
@RequestMapping("/travel")
public class TravelController {

    @Autowired
    private ITravelService travelService;
    

    // 区域显示
    // 这里需要的是分页的数据
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") TravelQuery qo){
        Page<Travel> page = travelService.query(qo);
        model.addAttribute("page",page);
        return "/travel/list";
    }


    
//    /**
//     * 新增和修改
//     * 判断对象中是否有Id来确定执行新增还是修改方法
//     * @param travel
//     * @return
//     */
//    @RequestMapping("/saveOrUpdate")
//    @ResponseBody
//    public Object saveOrUpdate(Travel travel){
//        if (StringUtils.hasLength(travel.getId())){
//            travelService.update(travel);
//        }else{
//            travelService.save(travel);
//        }
//        return JsonResult.success();
//    }
//
//
//    /**
//     * 根据传上来的id删除
//     * @param id
//     * @return
//     */
//    @RequestMapping("/delete")
//    @ResponseBody
//    public Object delete(String id){
//        if (StringUtils.hasLength(id)) {
//            travelService.delete(id);
//        }
//        return JsonResult.success();
//    }


    /**
     * 点击查看游记详情
     * @param id
     * @return
     */
    @RequestMapping("/getContentById")
    @ResponseBody
    public Object getContentById(String id){
        Travel travel = travelService.get(id);
        String content = travel.getContent();
        return JsonResult.success(content);
    }


    /**
     * //发布/拒绝
     * @param id
     * @param state
     * @return
     */
    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(String id ,Integer state){

        travelService.changeState(id,state);

        return JsonResult.success();
    }




}
